package 多线程和队列.多任务执行.Fututre;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author Amos  2022/12/4
 */
public class FutrueAsyncErrorDemo {
        static ExecutorService executor =
                Executors.newCachedThreadPool();
        //接收文章名称，获取并计算文章分数
        public static int getArticleScore(String name){
            Future<Integer> futureA =
                    executor.submit(new CalculateArticleScoreA1());
            Future<Integer> futureB =
                    executor.submit(new CalculateArticleScoreB1());

            Future<Integer> futureC =
                    executor.submit(new CalculateArticleScoreC1());
            doSomeThingElse();
            Integer a = 0;
            try {
                a = futureA.get();
            } catch (InterruptedException e) {
                futureA.cancel(true);
                e.printStackTrace();
            } catch (ExecutionException e) {
                futureA.cancel(true);
                e.printStackTrace();
            }
            Integer b = 0;
            try {
                b = futureB.get();
            } catch (InterruptedException e) {
                futureB.cancel(true);
                e.printStackTrace();
            } catch (ExecutionException e) {
                futureB.cancel(true);
                e.printStackTrace();
            }
            Integer c = 0;
            try {
                c = futureC.get();
            } catch (InterruptedException e) {

                futureC.cancel(true);
                e.printStackTrace();
            } catch (ExecutionException e) {
                futureC.cancel(true);
                e.printStackTrace();
            }
            executor.shutdown();
            return a+b+c;
        }
        private static void doSomeThingElse() {
            System.out.println("exec otherthings");
        }
        public static void main(String[] args) {
            System.out.println(getArticleScore("demo"))
            ;
        }
    }
    class CalculateArticleScoreA1 implements
            Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            Random random = new Random();
            TimeUnit.SECONDS.sleep(10);

            System.out.println(Thread.currentThread().getName());
            return random.nextInt(100);
        }
    }
    class CalculateArticleScoreB1 implements
            Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            Random random = new Random();
            TimeUnit.SECONDS.sleep(20);
            System.out.println(Thread.currentThread().getName());
            return random.nextInt(100);
        }
    }
    class CalculateArticleScoreC1 implements
            Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            Random random = new Random();
            TimeUnit.SECONDS.sleep(30);
            System.out.println(Thread.currentThread().getName());
            return random.nextInt(100);
        }
        }

